<?php
/**
 * 管理员模块控制器
 * @Author    紫梦
 * @DateTime  2017-11-19
 * @copyright 响响租车
 * @version   1.0
 */
namespace app\index\controller;

use think\Request;
use app\common\model\Admin as AdminModel;
use app\common\model\Roles;
use app\common\model\Agency;

class Admin extends Base
{
	/**
	 * 管理员列表
	 */
	public function index()
	{

		
		$model = AdminModel::with('agency')->select();
		
		$this->assign('data',$model);

		return $this->fetch();
  
	}

	/**
	 * 添加管理员 
	 */
	public function add()
	{
		$request = Request::instance();

		if($request->isAjax()){
			$data = $request->post();
			$data['password'] = password_hash($data['password'],PASSWORD_DEFAULT);

			$model = new AdminModel();
			
			try{
			   
				$model->allowField(true)->save($data);

			}catch(\Exception $e){
			    return [
					'code'=>2,
					'msg'=>$e->getMessage(),
				];
			}

			return [
                'code'=>1,
                'msg'=>'添加成功'
                ];
		}else{
			$roles = Roles::all();
			$agency = Agency::all();
			$this->assign('roles',$roles);
			$this->assign('agency',$agency);
			return $this->fetch();

		}
	}

	/**
	 * 编辑管理员 
	 */
	public function edit($id=1)
	{
		$request = Request::instance();

		if($request->isAjax()){
			$data = $request->post();
			$model = new AdminModel();
			try{
			   
				$model->allowField(true)->isUpdate(true)->save($data);

			}catch(\Exception $e){
			    return [
					'code'=>2,
					'msg'=>$e->getMessage(),
				];
			}

			return [
                'code'=>1,
                'msg'=>'更新成功'
                ];
		}else{
			$roles = Roles::all();
			$agency = Agency::all();
			$admin = AdminModel::get($id);
			$this->assign('roles',$roles);
			$this->assign('agency',$agency);
			$this->assign('admin',$admin);
			return $this->fetch();

		}

	}

	public function password($id)
	{
		$request = Request::instance();

		if($request->isAjax()){
			$data = $request->post();
			$data['password'] = password_hash($data['password'],PASSWORD_DEFAULT);

			$model = new AdminModel();
			
			try{
			   
				$model->allowField(true)->isUpdate(true)->save($data);

			}catch(\Exception $e){
			    return [
					'code'=>2,
					'msg'=>$e->getMessage(),
				];
			}
			return [
                'code'=>1,
                'msg'=>'更新成功'
                ];
		}else{
			$admin = AdminModel::get($id);
			$this->assign('admin',$admin);
			return $this->fetch();

		}


	}

	public function status($id,$status)
    {
        try {
            AdminModel::where('id', $id)
                        ->update(['status' => $status]);
            
        } catch (\Exception $e) {
            return [
                'code'=>2,
                'msg'=>$e->getMessage()
                ];
            
        }
        
        return [
            'code'=>1,
            'msg'=>'更新成功'
            ];
    }

    public function delete($id)
    {
    	try {
            AdminModel::destroy($id);
            
        } catch (\Exception $e) {
            return [
                'code'=>2,
                'msg'=>$e->getMessage()
                ];
            
        }
        
        return [
            'code'=>1,
            'msg'=>'删除成功'
            ];
    }
    
}
